﻿package igz.fleaxy.ui.tab 
{
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import igz.fleaxy.Fleaxy;
	import igz.fleaxy.pattern.Dto;

	/**
	 * ...
	 * @author Intelygenz | www.intelygenz.com
	 * 
	 * Clase abstracta referente al "botón" diferenciador de cada Tab (o contenedor de pestañas) estará contenido en un TabUi y se 
	 * ocupará de seleccionar su tab, arrastrarse, cerrarse y todas las acciones referente a este elemento
	 * 
	 */
	public class AbstractTabItemUi extends Sprite  {
		
		protected var _TabUi:AbstractTabUi;
		
		protected var _TabDto:Dto;
		public function get $TabDto () : Dto {
			return _TabDto;
		}

		protected var _Selected:Boolean;
		
		private   var _MousePoint_StartDrag:Point;
		protected var _MouseMoveX:Number;
		protected var _MouseMoveY:Number;

		/**
		 * Constructor ocupado de generar el TabItem, al que se le pasa el AbstractTabUi que lo contendrá y el DTO con los datos
		
		 * @param	p_tabUi TabUi que lo contiene
		 * @param	p_tabDto DTO con los datos
		 */
		public function AbstractTabItemUi(p_tabUi:AbstractTabUi, p_tabDto:Dto) {
			_TabUi  = p_tabUi;
			_TabDto = p_tabDto;

			_AddEventListeners();

			this.buttonMode    = true;
			this.tabEnabled	   = false;
		}

		protected function _OnRollOver(p_mouseEvent:MouseEvent )  :void {
			
			if ( !_Selected ) {
				$On();
			}
		}

		protected function _OnRollOut(p_mouseEvent:MouseEvent )  :void {
			
			if ( !_Selected ) {
				$Off();
			}
		}

		protected function _StartDrag() : void {
			_MousePoint_StartDrag = new Point( mouseX, mouseY );
			Fleaxy.$Current.$Stage.addEventListener( MouseEvent.MOUSE_MOVE, _MouseMove );
			Fleaxy.$Current.$Stage.addEventListener( MouseEvent.MOUSE_UP, _MouseUp );
		}

		protected function _MouseMove(p_mouseEvent:MouseEvent) : void {
			_MouseMoveX = parent.mouseX - _MousePoint_StartDrag.x;
			_MouseMoveY = parent.mouseY - _MousePoint_StartDrag.y;
		}

		protected function _MouseUp(p_mouseEvent:MouseEvent) : void {
			Fleaxy.$Current.$Stage.removeEventListener( MouseEvent.MOUSE_MOVE, _MouseMove );
			Fleaxy.$Current.$Stage.removeEventListener( MouseEvent.MOUSE_UP, _MouseUp );
		}

		public function $Select() : void {
			$On();
			_Selected = true;
		}

		public function $Unselect() : void {
			_Selected = false;
			$Off();
		}

		public function $On() : void {

		}
	
		public function $Off() : void {

		}
		
		public function $Enable () : void {
			_AddEventListeners();
			useHandCursor = true;
		}
		
		public function $Disable () : void {
			_RemoveEventListeners();
			useHandCursor = false;
		}

		protected function _AddEventListeners () : void {
			addEventListener(MouseEvent.ROLL_OVER, _OnRollOver);
			addEventListener(MouseEvent.ROLL_OUT, _OnRollOut);
		}
		
		protected function _RemoveEventListeners () : void {
			removeEventListener(MouseEvent.ROLL_OVER, _OnRollOver);
			removeEventListener(MouseEvent.ROLL_OUT, _OnRollOut);
		}
	}
}